package evolutionaryballoons;

import static evolutionaryballoons.Configuration.*;

import java.awt.geom.Point2D;

public class Balloon {
	
	/* Relations */
	
	private Points points;
	private Currents currents;
	
	/* Properties */
	
	public double x;
	public double y;
	public float lift;
	public boolean burnerOn;
	public int targetPoint;
	public int cost;
	
	public Balloon(Currents currents, Points points) {
		this.currents = currents;
		this.points = points;
		this.targetPoint = 0;
		this.cost = 0;
		
		x = sStartingPointX;
		y = sStartingPointY;
		burnerOn = false;
	}
	
	public void act() {
		if (burnerOn) {
			lift = Math.min(lift + sHeatingFactor, sMaxLift);
		} else {
			lift = Math.max(lift - sCoolingFactor, sMinLift);
		}
	}
	
	public void drift() {
		float wind = currents.getWindAt(y);
		
		// Move
		
		x += wind;
		y -= lift;
		
		// Collect point ?
		
		// Point2D p = points.getClosestPoint(x, y);
		Point2D p = points.get(targetPoint);
		if (p != null && p.distance(x, y) < sDistanceToCollectPoint) {
			points.remove(p);
		}
	}
	
	public boolean isInWorld() {
		return ((x >= 0) && (x < sWidth) && (y >= 0) && (y < sHeight));
	}
	
	public void writeLog() {
		String message = String.format("[%.0f, %.0f] Heat: %s, Burner: %s", x, y, lift, burnerOn);
		System.out.println(message);
	}
}
